OUT := out
EXT_MP4 := mp4
EXT_PIC := png

# 1. 单文件取首帧
# 用法：make frame VIDEO=foo.mp4
frame:
	@mkdir -p $(OUT)
	ffmpeg -hide_banner -loglevel error -i "$(VIDEO)" -vframes 1 -q:v 2 "$(OUT)/$(basename $(VIDEO))_frame.png"

# 2. 单图片减半
# 用法：make half PIC=foo.png
half:
	@mkdir -p $(OUT)
	convert "$(PIC)" -resize 50% "$(OUT)/$(basename $(PIC))_half.png"

# 3. 批量首帧
frames:
	@mkdir -p $(OUT)
	for f in *.$(EXT_MP4); do \
		[ -e "$$f" ] || continue; \
		out="$(OUT)/$${f%.*}_frame.png"; \
		ffmpeg -hide_banner -loglevel error -i "$$f" -vframes 1 -q:v 2 "$$out"; \
	done

# 4. 批量减半
# 批量生成半尺寸图片（关闭循环回显 @set +，最后重新打开set -x并打印 done）
halfs:
	@mkdir -p $(OUT)
	@set +x; \
	for f in *.$(EXT_PIC); do \
		[ -e "$$f" ] || continue; \
		out="$(OUT)/$${f%.*}_half.png"; \
		convert "$$f" -resize 50% "$$out"; \
	done && \
	set -x ;\
	echo done

clean:
	rm -rf $(OUT)

.PHONY: frame half frames halfs clean